<template>
  <el-form ref="form" :model="user" :rules="rules" label-width="120px">
    <el-form-item label="供应商名称" prop="corporateName">
      <el-input v-model="user.corporateName" maxlength="30" />
    </el-form-item>
    <el-form-item label="统一信用代码" prop="unifiedCreditCode">
      <el-input v-model="user.unifiedCreditCode" maxlength="30" />
    </el-form-item>
    <el-form-item label="法人名称" prop="legalPerson">
      <el-input v-model="user.legalPerson" maxlength="30" />
    </el-form-item>
    <el-form-item label="法人号码" prop="legalPhone">
      <el-input v-model="user.legalPhone" maxlength="30" />
    </el-form-item>
    <el-form-item label="公司地址" prop="address">
      <el-input v-model="user.address" maxlength="30" />
    </el-form-item>
    <el-form-item label="公司类别" prop="type">
      <el-select v-model="user.type" placeholder="公司类型">
            <el-option
              v-for="dict in companyOptions"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
    </el-form-item>
    <el-form-item label="公司邮箱" prop="email">
      <el-input v-model="user.email" maxlength="30" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="mini" @click="submit">保存</el-button>
      <el-button type="danger" size="mini" @click="close">关闭</el-button>
    </el-form-item>
  </el-form>
</template>
  
  <script>
import { updateSupply } from "@/api/system/user";

export default {
  props: {
    user: {
      type: Object,
    },
  },
  data() {
    const validateEmail = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请正确填写邮箱"));
      } else {
        var reg =
          /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
        if (!reg.test(value)) {
          callback(new Error("请输入有效的邮箱"));
          return;
        }
        callback();
      }
    };
    const validatePhone = (rule, value, callback) => {
      const regExp =
        /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/;
      if (!regExp.test(value)) {
        callback(new Error("请输入正确的手机号"));
      } else {
        callback();
      }
    };
    return {
      companyOptions: [
        {
          value: '0',
          label: "办公用品类公司",
        },
        {
          value: '1',
          label: "承接工程类公司",
        },
      ],
      // 表单校验
      rules: {
        corporateName: [
          { required: true, message: "用户昵称不能为空", trigger: "blur" },
        ],
        unifiedCreditCode: [
          { required: true, message: "统一信用代码不能为空", trigger: "blur" },
        ],
        legalPerson: [
          { required: true, trigger: "blur", message: "请输入法人联系人" },
        ],
        legalPhone: [
          { required: true, validator: validatePhone, trigger: "blur" },
          { required: true, trigger: "blur", message: "请输入法人手机号" },
        ],
        address: [{ required: true, trigger: "blur", message: "请输入地址" }],
        type: [{ required: true, trigger: "blur", message: "请选择公司类型" }],
        email: [
          { required: true, message: "邮箱地址不能为空", trigger: "blur" },
          {
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: ["blur", "change"],
          },
        ],
        // phonenumber: [
        //   { required: true, message: "手机号码不能为空", trigger: "blur" },
        //   {
        //     pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
        //     message: "请输入正确的手机号码",
        //     trigger: "blur"
        //   }
        // ]
      },
    };
  },
  methods: {
    submit() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          const data = {}
          data.corporateName = this.user.corporateName
          data.unifiedCreditCode = this.user.unifiedCreditCode
          data.legalPerson = this.user.legalPerson
          data.legalPhone = this.user.legalPhone
          data.address = this.user.address
          data.type = this.user.type
          data.email = this.user.email
          updateSupply(data).then((response) => {
            this.$modal.msgSuccess("修改成功");
          });
        }
      });
    },
    close() {
      this.$tab.closePage();
    },
  },
};
</script>
  
